package com.djsearch.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.djsearch.R;
import com.djsearch.bean.ForPurchase;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.data.ImageManager;
import com.djsearch.data.RequestImage;
import com.djsearch.util.AsyncImageLoader;
import com.djsearch.util.AsyncImageLoader.ImageCallback;
/**
 * 淘购页面listview适配器
 * @author chenyang2
 *
 */
public class TaoGouListViewAdapter extends BaseAdapter {
	
	private Context mContext;
	private List<ForPurchase> taoGouGoodsList;
	private Bitmap mbitmap;
	public TaoGouListViewAdapter(Context context,List<ForPurchase> list) {
		mContext = context;
		taoGouGoodsList = list;
		Bitmap bitmap = ImageManager.drawableToBitmap(mContext.getResources().getDrawable(R.drawable.taogou),150.0f);
		mbitmap = (ImageManager.resizeImage(bitmap, 150.0f));
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return taoGouGoodsList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return taoGouGoodsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	
	public void refreshData(List<ForPurchase> list)
	{
		taoGouGoodsList = list;
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if (convertView == null) {
			LayoutInflater layoutInflater = ((Activity) mContext).getLayoutInflater();
			convertView = layoutInflater.inflate(R.layout.taogou_listview_item, parent, false);
			convertView.setTag(convertView);
		} else {
			convertView = (View) convertView.getTag();
		}
		//商品图片
		ImageView goodsImg = (ImageView)convertView.findViewById(R.id.taoGoodsImg);
//		asyncImageLoader.loadDrawable(goodsImg,
//				taoGouGoodsList.get(position).getSmallImage(),new ImageCallback(){
//			@Override
//			public void imageLoaded(ImageView imageView,
//					Drawable imageDrawable, String imageUrl, String tag) {
//				// TODO Auto-generated method stub
////				imageView.setImageBitmap(ImageManager.drawableToBitmap(imageDrawable,150.0f));
//			}
//			
//		});
		RequestImage.getIntance(mbitmap, "TaoGou").download(taoGouGoodsList.get(position).getSmallImage(), 
				goodsImg);
		//商品价格
		TextView goodsPrice = (TextView)convertView.findViewById(R.id.taoPriceText);
		goodsPrice.setText(taoGouGoodsList.get(position).getNowPrice());
		//商品原价
		TextView goodsOldPrice = (TextView)convertView.findViewById(R.id.yuanJiaText);
		goodsOldPrice.setText(taoGouGoodsList.get(position).getSalePrice());
		//商品描述
		TextView goodsDescribe = (TextView)convertView.findViewById(R.id.taoGoodsDescribe);
		goodsDescribe.setText(taoGouGoodsList.get(position).getDescription());
		//已经购买人数
		TextView buyedPersonNumber = (TextView)convertView.findViewById(R.id.gouMaiPersonsText);
		buyedPersonNumber.setText(taoGouGoodsList.get(position).getHasBuy());
		//配送地点
		TextView sendToPlace = (TextView)convertView.findViewById(R.id.peiSongText);
		
		
		return convertView;
	}





}
